package com.bjsxt.userscenter.common.util;

import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Component;

/**
 * 正则表达式
 * @author wangshSxt
 *
 */

@Component("regexUtil")
public class RegexUtil
{
	private String pattern_$="(\\$\\{)(\\w)(\\})";
	/**
	 * 生成随机数
	 * @param length	指定长度
	 * @return	返回随机数
	 */
	public String proccedStr(int length)
	{
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for(int i = 0 ; i < length ; i ++)
		{
			int randInt = random.nextInt(ConstatFinalUtil.ALLSTR.length());
			char ch = ConstatFinalUtil.ALLSTR.charAt(randInt);
			sb.append(ch);
		}
		return sb.toString() ; 
	}
	
	public String replace$(String souStr,Map<String,String> paramsMap){
		StringBuffer sb=new StringBuffer();
		Pattern pattern=Pattern.compile(pattern_$);
		Matcher matcher=pattern.matcher(souStr);
		while(matcher.find()){
			String res=matcher.group(2);
			if(paramsMap.get(res) != null){
				matcher.appendReplacement(sb,paramsMap.get(res));
			}
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
	
	
	
	
	
	
}
